﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using 真空印刷机定位视觉系统.窗口;

namespace 真空印刷机定位视觉系统
{
    public enum AcessLevel
   {
     操作员 = 0,
     工程师 = 1,
     开发者 = 2,
     Admin = 3,
   };


    public partial class Form1 : Form
    {
        private delegate void ExceptionDelegate(Exception ex, String ExMsg);
        public event ExceptionDelegate  ExceptionEvent;

        public Form1()
        {
            InitializeComponent();
        }
        #region  安全权限设置

        private void AnQuanQuanXian(AcessLevel Level)
        {
            AcessLevel_1(Level);
            AcessLevel_2(Level);
            AcessLevel_3(Level);
            AcessLevel_4(Level);
            UserLevel = Level;
            ToolStripStatusLabel2.Text = Level.ToString();
        }
        private void AcessLevel_1(AcessLevel Level)
        {
            bool ControlEnable = GetControlEnbleFromLevel(AcessLevel.操作员, Level);
            视觉测试ToolStripMenuItem.Visible = ControlEnable;
            ToolStrip1.Enabled = ControlEnable;
        }
        private void AcessLevel_2(AcessLevel Level)
        {
            bool ControlEnable = GetControlEnbleFromLevel(AcessLevel.工程师 , Level);
             //调试
             相机一视觉ToolStripMenuItem.Enabled = ControlEnable;
             相机二视觉ToolStripMenuItem.Enabled = ControlEnable;
             cksaveimg.Enabled = ControlEnable;
        }
        private void AcessLevel_3(AcessLevel Level)
        {
            bool ControlEnable = GetControlEnbleFromLevel(AcessLevel.开发者 , Level);
             //保存功能权限
            项目操作ToolStripMenuItem.Visible = ControlEnable;
            视觉工具ToolStripMenuItem.Visible = ControlEnable;
            视觉测试ToolStripMenuItem.Visible = ControlEnable;
        }
        private void AcessLevel_4(AcessLevel Level)
        {
            bool ControlEnable = GetControlEnbleFromLevel(AcessLevel.Admin , Level);
            相机调试ToolStripMenuItem.Visible = ControlEnable;

        }
        private bool GetControlEnbleFromLevel(AcessLevel ControlLevel, AcessLevel Level)
        {
            bool ControlEnble = false;
            if (Level >= ControlLevel)
            {
                return !ControlEnble;
            }
            return ControlEnble;
        }
        #endregion

        #region  用户：登入与注销
        public AcessLevel UserLevel = AcessLevel.操作员;
        private void 登入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Frm_Login frmLogin = new Frm_Login();
                if (frmLogin.ShowDialog() == DialogResult.OK)
                {
                    AnQuanQuanXian(frmLogin.Level);
                    ShowLoginOrLogOut(frmLogin.Level);
                    ToolStripStatusLabel2.Text = frmLogin.Level.ToString();
                    ShowUserOperatorMessage(string.Format("{0}登录成功", UserLevel.ToString()),Color.Black);
                }
                else
                    ShowUserOperatorMessage(string.Format("{0}登录失败", UserLevel.ToString()), Color.Red);
            }
            catch (Exception ex)
            {
                ShowUserOperatorMessage(string.Format("{0}登录失败", UserLevel.ToString()), Color.Red, ex);
            }
        }
        private void 注销ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string UserName = UserLevel.ToString();
            try
            {
                ShowUserOperatorMessage("注销成功", Color.Black);
                AnQuanQuanXian(AcessLevel.操作员);
                ShowLoginOrLogOut(AcessLevel.操作员);
                ShowUserOperatorMessage(string.Format("{0}注销成功！", UserName), Color.Black);
            }
            catch (System.Exception ex)
            {
                ShowUserOperatorMessage(string.Format("{0}注销成功！原因：{1}", UserName, ex.Message), Color.Red, ex);
            }
        }

        /// <summary>
        /// 操作员权限没有注销的权限的
        /// </summary>
        /// <param name="Level"></param>
        private void ShowLoginOrLogOut(AcessLevel Level)
        {
            bool IsShowLogin = true;
            if (Level > AcessLevel.操作员)
            {
                IsShowLogin = false;
            }
            登入ToolStripMenuItem.Enabled = IsShowLogin;
            注销ToolStripMenuItem.Enabled = !IsShowLogin;
        }

        #endregion


        #region 显示事件
           public void ShowUserOperatorMessage(String Message ,Color TxtColor ,Exception ex = null)
           {
                Color OldColor = RTxt_OperatorMsg.ForeColor;
                RTxt_OperatorMsg.SelectionColor = TxtColor ;
                string Usermsg = string.Format("[用户:{0}]:\n", UserLevel.ToString());
                string timeStr = string.Format("{0:yyyy/MM/dd HH:mm:ss}", DateTime.Now );
                timeStr = string.Format ("操作时间:{0}\n",timeStr );
                RTxt_OperatorMsg.AppendText(string.Format ("{0}{1}{2}\n",Usermsg,timeStr,Message));
                RTxt_OperatorMsg.SelectionColor = OldColor;
                RTxt_OperatorMsg.ScrollToCaret();
               if (ex != null)
               {
                   ExceptionEvent(ex, Message);
               }
           }
      
        #endregion


        #region 初始化事件
           private void Form1_Load(object sender, EventArgs e)
           {

           }

        #endregion
      
       
    }
}
